package client.control;

import base.BaseValue;
import client.UserState;
import client.entity.Good;
import client.entity.User;
import client.util.ImageUtil;
import com.alibaba.fastjson.JSONObject;

import javax.swing.*;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
  * @Description 用户有关的网络接口封装
  * @author 孔明浩
  * @Date 2020/4/8
  */
public class UserControl {
    Executor executor = Executors.newSingleThreadExecutor();
    UserControl(){};
    public ResponseEntity register(String username, String password){
        return baseAction(username, password, "register");
    }

    public ResponseEntity login(String username, String password){
        return baseAction(username, password, "login");
    }

    public ResponseEntity getUserInfo(String token){
        ConnectControl connectControl = new ConnectControl();
        connectControl.doPost(BaseValue.load + "getUserInfo", token, null);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity setUserInfo(String token, String username){
        Map<String, String> params = new HashMap<>();
        params.put("userName", username);
        ConnectControl connectControl = new ConnectControl();
        connectControl.doPost(BaseValue.load + "setUserInfo", token, params);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity setHeadPic(String token, File file){
        Map<String, File> params = new HashMap<>();
        params.put("pic", file);
        ConnectControl connectControl = new ConnectControl();
        connectControl.doPost(BaseValue.load + "setHeadPic", token, null, params);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity modifyPassword(String token, String oldPassword, String newPassword){
        Map<String, String> params = new HashMap<>();
        params.put("p1",oldPassword);
        params.put("p2",newPassword);
        ConnectControl connectControl = new ConnectControl();
        connectControl.doPost(BaseValue.load + "modifyPassword", token, params);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity modifyGold(String token, int gold){
        Map<String, String> map = new HashMap<>();
        map.put("gold", gold + "");
        ConnectControl connectControl = new ConnectControl();
        connectControl.doPost(BaseValue.load + "modifyGold", token, map);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity getGold(String token){
        ConnectControl connectControl = new ConnectControl();
        connectControl.doPost(BaseValue.load + "getGold",token,null);
        return ResponseEntity.fit(connectControl);
    }

    public void logout(String token){
        ConnectControl connectControl = new ConnectControl();
        connectControl.doPost(BaseValue.load + "logout",token,null);
    }

    public void deleteAccount(String token){
        ConnectControl connectControl = new ConnectControl();
        connectControl.doPost(BaseValue.load + "deleteAccount", token, null);
    }

    public ResponseEntity getOtherUserInfo(int userId){
        ConnectControl connectControl = new ConnectControl();
        Map<String, String> map = new HashMap<>();
        map.put("userId", userId + "");
        connectControl.doPost(BaseValue.load + "getOtherUserInfo", null, map);
        return ResponseEntity.fit(connectControl);
    }

    /**
     * @Description 登录注册的底层
     * @param username 用户名
     * @param password 密码
     * @param action 行为，登录或者注册
     * @author 孔明浩
     * @Date 2020/4/7
     */
    private ResponseEntity baseAction(String username, String password, String action) {
        Map<String, String> params = new HashMap<>();
        params.put("userName", username);
        params.put("password", password);
        ConnectControl connectControl = new ConnectControl();
        connectControl.doPost(BaseValue.load + action, null, params);
        return ResponseEntity.fit(connectControl);
    }

    /**
     * 获取头像, 同步方法方法
     * @param token token
     * @param size 尺寸大小
     * @return 头像
     */
    public ImageIcon getHeadImage(String token, int size) {
        ResponseEntity responseEntity = getUserInfo(token);
        ImageIcon imageIcon = new ImageIcon();
        if (responseEntity.code == 0) {
            User user = JSONObject.parseObject(responseEntity.data, User.class);
            UserState.setUser(user);
            //获取图片并格式化大小
            imageIcon = getImageIcon(size, imageIcon, user);
        }
        return imageIcon;
    }

    /**
     * 获取他人的头像，并格式化大小
     * @param userId 用户id
     * @param size 头像大小，长和宽的较小值
     * @return 头像
     */
    public ImageIcon getHeadImage(int userId, int size){
        ResponseEntity responseEntity = getOtherUserInfo(userId);
        ImageIcon imageIcon = new ImageIcon();
        if (responseEntity.code == 0) {
            User user = JSONObject.parseObject(responseEntity.data, User.class);
            //获取图片并格式化大小
            imageIcon = getImageIcon(size, imageIcon, user);
        }
        return imageIcon;
    }

    /**
     * 通过商品Id获取用户Id
     * @param goodId 商品Id
     * @return 用户Id
     */
    public int getUserIdByGoodId(int goodId){
        int userId = 0;
        ResponseEntity responseEntity = Controller.GOOD_CONTROL.getGoodInfo(goodId);
        if (responseEntity.getCode() == 0){
            Good good = JSONObject.parseObject(responseEntity.getData(), Good.class);
            responseEntity = Controller.STORE_CONTROL.getUserIdByStoreId(good.getStoreId());
            userId = Integer.parseInt(responseEntity.getData());
        }
        return userId;
    }
    private static ImageIcon getImageIcon(int size, ImageIcon imageIcon, User user) {
        try {
            imageIcon = new ImageIcon(new URL(user.getPic()));
            if (imageIcon.getIconHeight() > imageIcon.getIconWidth()) {
                imageIcon = ImageUtil.getImageIcon(imageIcon, null, 0, size);
            } else {
                imageIcon = ImageUtil.getImageIcon(imageIcon, null, size, 0);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return imageIcon;
    }

}
